package com.miuugo.minimall.product.app;

import com.miuugo.common.utils.PageUtils;
import com.miuugo.common.utils.R;
import com.miuugo.common.valid.AddGroup;
import com.miuugo.common.valid.UpdateGroup;
import com.miuugo.common.valid.UpdateStatusGroup;
import com.miuugo.minimall.product.entity.BrandEntity;
import com.miuugo.minimall.product.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 品牌
 * @author yu_zhong
 * @email 2211735722@qq.com
 * @date 2021-08-24 15:21:23
 */
@Api(tags="品牌管理")
@RestController
@RequestMapping("product/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 列表
     *
     * @param params 参数个数
     * @return {@link R}
     */
    @ApiOperation(value = "品牌列表")
    @ApiImplicitParam(name = "params",value = "参数", required = true,paramType = "path")
    @RequestMapping("/list")
    //    @RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     *
     * @param brandId 品牌标识
     * @return {@link R}
     */
    @RequestMapping("/info/{brandId}")
    //    @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     *
     * @param brand 品牌
     * @return {@link R}
     */
    @RequestMapping("/save")
    //    @RequiresPermissions("product:brand:save")
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand) {
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 更新
     * 修改
     * 更新品牌，还要更新相关关系表的冗余字段
     *
     * @param brand 品牌
     * @return {@link R}
     */
    @RequestMapping("/update")
    //    @RequiresPermissions("product:brand:update")
    public R update(@Validated(UpdateGroup.class) @RequestBody BrandEntity brand) {
        brandService.updateDetail(brand);

        return R.ok();
    }

    /**
     * 更新品牌地位
     * 修改状态
     *
     * @param brand 品牌
     * @return {@link R}
     */
    @RequestMapping("/updateBrandStatus")
    //    @RequiresPermissions("product:brand:update")
    public R updateBrandStatus(@Validated(UpdateStatusGroup.class) @RequestBody BrandEntity brand) {
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     *
     * @param brandIds 品牌标识
     * @return {@link R}
     */
    @RequestMapping("/delete")
    //    @RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
